/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.dao.GoodsCategoryDao;
import com.thinkgem.jeesite.modules.shop.entity.GoodsCategory;

/**
 * 商品分类管理Service
 * @author xxs
 * @version 2016-03-21
 */
@Service
@Transactional(readOnly = true)
public class GoodsCategoryService extends TreeService<GoodsCategoryDao, GoodsCategory> {

	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	
	public GoodsCategory get(String id) {
		return super.get(id);
	}
	
	public List<GoodsCategory> findList(GoodsCategory goodsCategory) {
		if (StringUtils.isNotBlank(goodsCategory.getParentIds())){
			goodsCategory.setParentIds(","+goodsCategory.getParentIds()+",");
		}
		return super.findList(goodsCategory);
	}
	
	@Transactional(readOnly = false)
	public void save(GoodsCategory goodsCategory) {
		if (StringUtils.isNotBlank(goodsCategory.getId())){
			goodsCategoryDao.deleteGoodsCategoryBrand(goodsCategory);
			if (goodsCategory.getBrandList() != null && goodsCategory.getBrandList().size() > 0){
				goodsCategoryDao.insertGoodsCategoryBrand(goodsCategory);
			}else{
				throw new ServiceException(goodsCategory.getName() + "没有关联品牌！");
			}
		}
		super.save(goodsCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(GoodsCategory goodsCategory) {
		super.delete(goodsCategory);
	}
	
	public GoodsCategory getGoodsCategoryAndBandList(GoodsCategory goodsCategory) {
		return goodsCategoryDao.getGoodsCategoryAndBandList(goodsCategory.getId());
	}
	
	public List<GoodsCategory> getGoodsCategoryListByParentId(String parentId) {
		return goodsCategoryDao.getGoodsCategoryListByParentId(parentId);
	}
	
	/**
	 * 通过编号获取栏目列表
	 */
	public List<GoodsCategory> findByIds(String ids) {
		List<GoodsCategory> list = Lists.newArrayList();
		String[] idss = StringUtils.split(ids,",");
		if (idss.length>0){
//			List<Category> l = dao.findByIdIn(idss);
//			for (String id : idss){
//				for (Category e : l){
//					if (e.getId().equals(id)){
//						list.add(e);
//						break;
//					}
//				}
//			}
			for(String id : idss){
				GoodsCategory e = goodsCategoryDao.get(id);
				if(null != e){
					//System.out.println("e.id:"+e.getId()+",e.name:"+e.getName());
					list.add(e);
				}
				//list.add(dao.get(id));
				
			}
		}
		return list;
	}
	
}